package com.example.myapplication.Garbagesorting;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.CarouselGarbageBean;
import com.example.myapplication.ShuJu.NewsCategoriesBean;
import com.example.myapplication.ShuJu.NewscategorylistBean;
import com.example.myapplication.apter.CarouselGarbageApter;
import com.example.myapplication.apter.NewscategorylistApter;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import java.util.ArrayList;


@SuppressWarnings("all")
public class GarbageActivity extends AppCompatActivity {

    private Button outpatient_bloak;
    private ViewPager outpatient_vpage;
    private TabLayout tab_garbage;
    private Button but_garbage01;
    private Button but_garbage02;
    private RecyclerView re_garbage;
    private NewsCategoriesBean newsCategoriesBean;
    private CarouselGarbageBean carouselGarbageBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_garbage);
        initview();
        //返回
        but_bloak();
        //轮播图
        carousel();
        //新闻分类
        newsCategories();
        newscategorylist(8);
        //搜索
        bingsearch();

    }

    private void binsort() {
        but_garbage02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GarbageActivity.this,SortSearchActivity.class);
                Gson gson = new Gson();
                String newsCategoriesBeanString = gson.toJson(carouselGarbageBean.getData().get(0));
                intent.putExtra("carouselGarbageBeanStringID",newsCategoriesBeanString);
                GarbageActivity.this.startActivity(intent);
            }
        });
    }

    private void bingsearch() {
        but_garbage01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GarbageActivity.this,SearchActivity.class);
                GarbageActivity.this.startActivity(intent);
            }
        });
    }

    //新闻分类
    private void newsCategories() {
       OKHttpTool.doNetRequest("prod-api/api/garbage-classification/news-type/list",
               OKHttpTool.GET,
               NewsCategoriesBean.class, new OKHttpTool.NetCallback() {
                   @Override
                   public void onSuccess(Object obj) {
                       newsCategoriesBean = (NewsCategoriesBean)obj;
                       if(newsCategoriesBean.getCode()==200){
                           runOnUiThread(new Runnable() {
                               @Override
                               public void run() {
                                   for (NewsCategoriesBean.RowsBean row : newsCategoriesBean.getRows()) {
                                       TabLayout.Tab tab = tab_garbage.newTab();
                                       tab.setText(row.getName());
                                       tab_garbage.addTab(tab);
                                       newssort(row.getId());
                                   }

                               }
                           });
                       }
                   }

                   @Override
                   public void onFailure(String msg) {

                   }
               });



    }

    private void newssort(Integer id) {
        tab_garbage.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String NewsName = tab.getText().toString();
                if(NewsName.equals("新时尚")){
                    newscategorylist(id);
                }else if(NewsName.equals("党员在行动")){
                    newscategorylist(id);
                }else if(NewsName.equals("分类达人")){
                    newscategorylist(id);
                }else if(NewsName.equals("社区动态")){
                    newscategorylist(id);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void newscategorylist(Integer id) {
        OKHttpTool.doNetRequest("/prod-api/api/garbage-classification/news/list?type="+id,
                OKHttpTool.GET,
                NewscategorylistBean.class, new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        NewscategorylistBean newscategorylistBean = (NewscategorylistBean)obj;
                        if(newscategorylistBean.getCode()==200){
                           runOnUiThread(new Runnable() {
                               @Override
                               public void run() {
                                   NewscategorylistApter newscategorylistApter = new NewscategorylistApter(GarbageActivity.this,newscategorylistBean.getRows());
                                   LinearLayoutManager linearLayoutManager = new LinearLayoutManager(GarbageActivity.this);
                                   re_garbage.setLayoutManager(linearLayoutManager);
                                   re_garbage.setAdapter(newscategorylistApter);
                               }
                           });
                        }
                    }
                    @Override
                    public void onFailure(String msg) {
                        Log.e("onFailure==>","onFailure");
                    }
                });
    }

    private void but_bloak() {
        outpatient_bloak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GarbageActivity.this.finish();
            }
        });
    }

    private void carousel() {
        OKHttpTool.doNetRequest("prod-api/api/garbage-classification/ad-banner/list", OKHttpTool.GET, CarouselGarbageBean.class, new OKHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                carouselGarbageBean = (CarouselGarbageBean)obj;
                if(carouselGarbageBean.getCode()==200){
                   runOnUiThread(new Runnable() {
                       @Override
                       public void run() {
                           CarouselGarbageApter carouselGarbage = new CarouselGarbageApter(GarbageActivity.this, carouselGarbageBean.getData());
                           outpatient_vpage.setAdapter(carouselGarbage);
                           mhandler.sendEmptyMessageDelayed(0,3*1000);
                           //分类
                           binsort();
                       }
                   });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void initview() {
        outpatient_bloak = findViewById(R.id.outpatient_bloak);
        outpatient_vpage = findViewById(R.id.outpatient_vpage);
        tab_garbage = findViewById(R.id.tab_garbage);
        but_garbage01 = findViewById(R.id.but_garbage01);
        but_garbage02 = findViewById(R.id.but_garbage02);
        re_garbage = findViewById(R.id.re_garbage);

    }


    private Handler mhandler =  new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what==0){
                int currentItem = outpatient_vpage.getCurrentItem();
                currentItem = (currentItem+1) % 3 ;
                outpatient_vpage.setCurrentItem(currentItem);
                mhandler.sendEmptyMessageDelayed(0,3*1000);
            }
        }
    };
}